package com.gms.service.eventmanager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gms.entity.event.Event;
import com.gms.mapper.eventmanager.EventMapper;
import com.gms.service.eventmanager.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*赛事
*Service层
*
 */
@Service
public class EventServiceImpl extends ServiceImpl<EventMapper, Event> implements EventService {

    @Autowired
    EventMapper eventMapper;


    //插入
    @Override
    public void insertEvent(Event event) {
        eventMapper.insert(event);
    }

    //查询全部
    @Override
    public Page<Event> queryAll(Integer PageNum){
        Page<Event> page=new Page(PageNum,8);
        return eventMapper.selectPage(page,null);
    }

    //根据id查询
    @Override
    public Event queryEvent_id(String event_id) {
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("event_id",event_id);
        return eventMapper.selectOne(queryWrapper);
    }

    //根据id-状态查询
    @Override
    public Page<Event> queryEvent_id_status(Integer PageNum,String event_id,String event_status) {
        Page<Event> page=new Page(PageNum, 8);
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        map.put("event_status",event_status);
        map.put("event_id",event_id);
        queryWrapper.allEq(map,false);
        return eventMapper.selectPage(page,queryWrapper);
    }

    //根据赛事状态查询
    @Override
    public Page<Event> queryEvent_status(Integer PageNum, String event_status){
        Page<Event> page=new Page(PageNum,8);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("event_status",event_status);
        return eventMapper.selectPage(page, queryWrapper);
    }

    //根据时间范围查询
    @Override
    public Page<Event> queryEvent_date(Integer PageNum, Date EventStartTime, Date EventEndTime){
        Page<Event> page=new Page(PageNum,8);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("event_start_time",EventStartTime,EventEndTime);
        return eventMapper.selectPage(page, queryWrapper);
    }

    //查询赛事进行中的
    @Override
    public Page<Event> queryEvent_in(Integer PageNum){
        Page<Event> page=new Page(PageNum,8);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        queryWrapper.ge("event_end_time",formatter.format(date));
        queryWrapper.le("event_start_time", formatter.format(date));
        queryWrapper.eq("event_status","审核通过");
        return eventMapper.selectPage(page, queryWrapper);
    }

    //查询待开始的
    @Override
    public Page<Event> queryEvent_before(Integer PageNum){
        Page<Event> page=new Page(PageNum,8);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        queryWrapper.gt("event_start_time", formatter.format(date));
        queryWrapper.eq("event_status","审核通过");
        return eventMapper.selectPage(page, queryWrapper);
    }

    //查询赛事结束的
    @Override
    public Page<Event> queryEvent_end(Integer PageNum){
        Page<Event> page=new Page(PageNum,8);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        queryWrapper.lt("event_end_time",formatter.format(date)).eq("event_status","审核通过")
                .or().eq("event_status","赛事取消");
        return eventMapper.selectPage(page, queryWrapper);
    }

    //根据名字模糊查询
    @Override
    public Page<Event> queryEvent_name(Integer PageNum, String event_name){
        Page<Event> page=new Page(PageNum,8);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("event_name",event_name);
        return eventMapper.selectPage(page, queryWrapper);
    }

    //查询，多字段
    @Override
    public Page<Event> queryEvent(Integer PageNum, String event_id,String event_name, Date EventStartTime, Date EventEndTime) {
        Page<Event> page=new Page(PageNum,8);
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        map.put("event_id",event_id);
        queryWrapper.allEq(map,false);
        queryWrapper.like("event_name",event_name);
        if(EventStartTime != null && EventEndTime != null){
            queryWrapper.between("event_start_time",EventStartTime,EventEndTime);
        }
        return eventMapper.selectPage(page, queryWrapper);
    }

    //查询用户赛事
    @Override
    public Page<Event> queryEvent_user(Integer PageNum, String user_id){
        Page<Event> page=new Page(PageNum,8);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Applicants",user_id);
        return eventMapper.selectPage(page,queryWrapper);
    }

    @Override
    public String queryEventId(){
        Integer sum = eventMapper.selectCount(null);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        String last = "limit "+(sum-1)+",1";
        queryWrapper.last(last);
        return eventMapper.selectOne(queryWrapper).getEventId();
    }

    @Override
    public List<Event> queryEventNotice(){
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Event> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("Notice",1);
        if(eventMapper.selectList(queryWrapper1).size() == 0) {
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            queryWrapper.ge("event_end_time", formatter.format(date));
            queryWrapper.le("event_start_time", formatter.format(date));
            queryWrapper.eq("event_status", "审核通过");
            String last = "limit 0,3";
            queryWrapper.last(last);
        } else {
            queryWrapper.eq("Notice",1);
        }
        return eventMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Event> queryEventNoticeAll(Integer PageNum){
        Page<Event> page=new Page(PageNum,8);
        QueryWrapper<Event> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Notice",1);
        return eventMapper.selectPage(page,queryWrapper);
    }

    @Override
    public void insertNotice(@Nullable String EventId){
        UpdateWrapper<Event> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("Event_id",EventId)
        .set("Notice",1);
        eventMapper.update(null,updateWrapper);
    }

    @Override
    public void deleteNotice(@Nullable String EventId){
        UpdateWrapper<Event> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("Event_id",EventId)
                .set("Notice",0);
        eventMapper.update(null,updateWrapper);
    }

    //赛事跟新
    @Override
    public void updateAuditor(Event event){
        eventMapper.updateById(event);
    }

}
